package files;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

import parser.I3tpParser;
import util.Candle;
import util.Util;

public class SgxDownloader {

	public SgxDownloader(String path) {
		try {
	        String content = getText("http://info.sgx.com/webprices.nsf/b37696ef37078e9848256aa7002bb7ec?OpenView"); 

	        String regex = "/webprices.nsf/stkprices/(\\d{4})-(\\d{2})-(\\d{2})/\\$File/SESPrice.dat";
	        Pattern p = Pattern.compile(regex); 
	        Matcher m = p.matcher(content);
	
	        while (m.find()) {
	        	String content1 = m.group(0);
	        	System.out.println(m.group(1) + " " + m.group(2) + " " + m.group(3) + " " + content1);
	        	URL url = new URL("http://info.sgx.com" + m.group(0));
	        	File file = new File(path + "SESPrice_" + m.group(1) + m.group(2) + m.group(3) + ".dat");
	        	FileUtils.copyURLToFile(url, file);
	        }
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
    private String getText(String url) throws Exception { 
        URL website = new URL(url); 
        URLConnection connection = website.openConnection(); 
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
 
        StringBuilder response = new StringBuilder(); 
        String inputLine; 
 
        while ((inputLine = in.readLine()) != null)  
            response.append(inputLine); 
 
        in.close(); 
 
        return response.toString(); 
    } 
 
    public static void main(String[] args) throws Exception { 
    	new SgxDownloader(args[0]);
    } 
}
